package com.object.configuration.config;

import cn.dev33.satoken.interceptor.SaRouteInterceptor;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Arrays;

@Log4j2
@Configuration
public class SaTokenConfigure implements WebMvcConfigurer {
    // 注册Sa-Token的注解拦截器，打开注解式鉴权功能
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 注册路由拦截器，自定义验证规则
        registry.addInterceptor(new SaRouteInterceptor((req, res, handler) -> {
            // 登录验证 -- 拦截所有路由，并排除/user/doLogin 用于开放登录
            SaRouter.match(Arrays.asList("/**"), Arrays.asList("/index/***","/didi/**","/test/**","/msg/****","/webjars/**","/doc.html/**","/swagger-ui.html/**"), () -> StpUtil.checkLogin());
        }));
    }
}